/*
 *绝对路径，相对路径工具类
 */
package com.wd.util;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @date 2011-10-11, 10:20:18
 * @author long@7road.com
 * @description PathUtil类：对路径操作，如相对转绝对，获取web根目录
 * 
 */
public class PathUtil {

	public String getAasolutePath(String path, boolean isWeb) throws IllegalAccessException {
		path = path.replace("/", "\\");
		if (path.indexOf(":") == -1) {
			if (isWeb) {
				return getWebInfPath() + "classes\\" + StringUtil.Trim(path, "\\");
			} else {
				String FileName = getWebClassesPath();
				FileName = FileName.substring(0, FileName.lastIndexOf("\\"));
				return FileName + "\\" + StringUtil.Trim(path, "\\");
			}
		} else {
			return path;
		}
	}

	public String getWebClassesPath() {
		String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		return StringUtil.subString(path.replace("/", "\\"), 1);

	}

	public String getWebInfPath() throws IllegalAccessException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF") + 8);
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}

	public String getWebRoot() throws IllegalAccessException {
		String path = getWebClassesPath();
		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF/classes"));
		} else {
			throw new IllegalAccessException("路径获取错误");
		}
		return path;
	}

	/**
	 * 通过传入的路径获取系统的绝对路径
	 * 
	 * @param path
	 *            相对路径或绝对路径
	 * @return
	 */
	public static String getAasolutePath(String path) {
		path = path.replace('\\', '/');
		if (path.startsWith("/") || path.startsWith(":/", 1)) {
			return path;
		} else {
			return String.format("%s/%s", System.getProperty("user.dir"), path).replace('\\', '/');
		}

	}

	public static String getResourcePath(String path) {
		if (!PathUtil.isAbsolute(path))
			path = SystemUtil.GAMEHOME +"/"+ path;
		else
			path = path.replace("\\", "/");
		return path;

	}

	public static boolean isAbsolute(String filePath) {
		return filePath.startsWith("/") || filePath.startsWith("\\") || filePath.indexOf(':') == 1;

	}

	/**
	 * 得到文件名字，不包括扩展后缀
	 * 
	 * @param filePath
	 * @return
	 */
	public static String getFileNameWithoutExtend(String filePath) {
		String simpleFileName = null;
		int end = filePath.lastIndexOf('.');
		int start1 = filePath.lastIndexOf('/');
		int start2 = filePath.lastIndexOf('\\');
		if (end != -1) {
			if (start1 == -1 && start2 == -1)
				simpleFileName = filePath.substring(0, end);
			else {
				simpleFileName = filePath.substring(start1 != -1 ? start1 + 1 : start2 + 1, end);
			}
		} else {
			if (start1 == -1 && start2 == -1)
				simpleFileName = filePath;
			else {
				simpleFileName = filePath.substring(start1 != -1 ? start1 + 1 : start2 + 1);
			}
		}
		return simpleFileName;
	}

	/**
	 * 得到文件名字,包含后缀
	 * 
	 * @param path
	 * @return
	 */
	public static String getFileName(String filePath) {
		String simpleFileName = null;
		int start1 = filePath.lastIndexOf('/');
		int start2 = filePath.lastIndexOf('\\');
		if (start1 == -1 && start2 == -1)
			simpleFileName = filePath;
		else {
			simpleFileName = filePath.substring(start1 != -1 ? start1 + 1 : start2 + 1);
		}

		return simpleFileName;
	}
}
